package com.xcx.robot.util;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.lang.GeoLocation;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.GpsDirectory;
import com.xcx.robot.common.UrlList;
import lombok.SneakyThrows;
import net.mamoe.mirai.utils.ExternalResource;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.util.Objects;

/**
 * @Author: 邢晨旭
 * @Date: 2021/5/24 20:21
 * @Description:
 */
public class ImageUtils {

    //TODO AK泄露
    final static String LONGITUDE_TO_ADDRESS_URL = UrlList.baidumap;

    @SneakyThrows
    public static void test(File jpegFile) {
        Metadata metadata = JpegMetadataReader.readMetadata(jpegFile);
        GpsDirectory gpsDirectory = metadata.getFirstDirectoryOfType(GpsDirectory.class);
        if (Objects.nonNull(gpsDirectory)) {
            GeoLocation geoLocation = gpsDirectory.getGeoLocation();
            System.out.println(geoLocation.getLongitude());
            System.out.println(geoLocation.getLatitude());
            System.out.println(HttpUtil.get(String.format(LONGITUDE_TO_ADDRESS_URL, geoLocation.getLatitude(), geoLocation.getLongitude())));
        }
//        //获取图片所有EXIF信息
//        Iterable<Directory> directories = metadata.getDirectories();
//        for (Directory directory : directories) {
//            for (Tag tag : directory.getTags()) {
//                System.out.println(tag);
//            }
//        }
    }


    public static void downloadImg(String url, Long group, long sender) {

        HttpUtil.downloadFile(url, Utils.getDesktopPath() + "image/" + group + "/" + DateUtils.getDateCode() + "/" + sender);
    }

    public static void main(String[] args) {
//        File file = new File("C:\\Users\\xcx\\Desktop\\身份证\\二寸.jpg");
//        test(file);
        downloadPicture("1",1L,1L,"111");
    }


    public static void downloadPicture(String imgUrl, Long group, Long sender, String fileName) {
        String path = Utils.getDesktopPath() + "/image/" + group + "/" + DateUtils.getDateCode() + "/" + sender + "/";
        URL url = null;
        try {
            File dir = new File(path);
            // 判断目录是否存在
            if (!dir.exists()) {
                //多层目录需要调用mkdirs
                dir.mkdirs();
            }
            url = new URL(imgUrl);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());
//            String suffix = HttpURLConnection.guessContentTypeFromStream(url.openStream());
            String suffix = ".png";
            FileOutputStream fileOutputStream = new FileOutputStream(new File(path + fileName + suffix));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            BASE64Encoder encoder = new BASE64Encoder();
            String encode = encoder.encode(buffer);//返回Base64编码过的字节数组字符串
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (Exception e) {

        }
    }

    public static ExternalResource initImage(BufferedImage bufferedImage) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, "png", os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ExternalResource.create(os.toByteArray());
    }

    public static ExternalResource initImage(String url) {
        try {
            return ExternalResource.create(new URL(url).openStream());
        } catch (Exception e) {
            return null;
        }
    }
    public static ExternalResource initImage(InputStream stream) {
        try {
            return ExternalResource.create(stream);
        } catch (Exception e) {
            return null;
        }
    }

    public static ExternalResource initImage(File file) {
        try {
            return ExternalResource.create(file);
        } catch (Exception e) {
            return null;
        }
    }

}
